#!/usr/bin/perl -w

##HACKABOT_HELP##
# Send a message
# !sayto #chan something to say
##HACKABOT_HELP##

use strict;

my ($msg, $nick);
while (<>) {
	if (/^msg\s*(.*)/) {
		$msg = $1;
	}
    elsif (/^nick\s+(.+)/) {
        $nick = $1;
    }
}

if (defined $msg) {
	if ($msg =~ /(#\S+)\s+(.+)/) {
        if (!nick_allowed($1, $nick)) {
            print "send You are not in that channel, so I won't talk for you in there.";
        } else {
		    print "to $1\n";
		    print "send $2\n";
        }
	}
	else {
		print "send Usage: !sayto #chan Some text to say";
	}
}

sub nick_allowed {
    my $chan = shift;
    my $nick = shift;
    my $name;

	my $names = `echo 'names $chan' | $ENV{'HACKABOT_DIR'}/scripts/client`;
	chomp $names;
	$names =~ s/^\S+\s+\S+\s+//;
	my @list = split(/\s+/, $names);
    foreach $name (@list) {
        return 1 if ($name eq $nick);
    }
    return 0;
}
